package com.shzu.jwc.ty.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {

		String url=request.getRequestURI();
		if(url.indexOf("login.action")>=0 || url.indexOf("loginfun.action")>=0) {
			return true;
		}
		HttpSession session=request.getSession();
		Integer usertype=(Integer) session.getAttribute("usertype");
		if(usertype==null || (usertype==0&&url.indexOf("jwc")<0) || (usertype==1&&url.indexOf("jwc")>=0)) {
			String path="/login.action";
			response.sendRedirect(request.getContextPath()+path);
			return false;
		}
		if(usertype==0&&url.indexOf("jwc")>=0) {
			return true;
		}
		else 
			return true;

	}

	
}
